<?php
/**
 * 配送员类
 * @package  htdocs
 * @subpackage  module/company
 * @author   create by allen.qiang
 * @version 1.0
 */

class cardFaceValue{

    /**
     *代金卡查询
     */

    public function search( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //代金卡id
        if (isset($arg_array['face_value_id']) && quanli_is_int($arg_array['face_value_id'])) {
            $sql .= ' AND face_value_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['face_value_id'];
        }

        //现金卡面值
        if (isset($_REQUEST['face_value']) && trim($_REQUEST['face_value']) != '') {
            $sql .= ' AND face_value = ?';
            $params_type .= 'i';
            $params_array[] = $_REQUEST['face_value'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM card_face_value ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM card_face_value ' . $sql;

            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'face_value_id'     => $item->face_value_id,
                    'face_value'   => $item->face_value,
                    'card_info'   => $item->card_info

                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 添加、修改现金卡
     *
     */
    public function editCashCard ( $arg_array ) {

        global $G_SHOP;

        //检查参数

        if (!isset( $arg_array['face_value']) && !is_numeric($arg_array['face_value_id'])){
            return array('message'=>'参数错误','code'=> 400);
        }

        if (empty($arg_array['face_value']))

            return array( 'code' => 400, 'message' => '代金卡面值不能为空' );



        $creator                          = $arg_array['creator'];
        $face_value_id                    = $arg_array['face_value_id'] ;
        $face_value                       = $arg_array['face_value'] ;
        $card_info                        = $arg_array['card_info'];    // 现金卡描述信息

        try{

            $card_master = new entity('card_face_value') ;
            if ($face_value_id  == 0) {
                $card_master->face_value = $face_value;
                if($G_SHOP->DBCA->fetch($card_master)){
                    throw new Exception('现金卡面值已存在', 404);
                }
                $card_master->creator = $creator;
            } else {
                $card_master->face_value_id = $face_value_id ;
                if (!$G_SHOP->DBCA->fetch($card_master)) {
                    throw new Exception('现金卡信息没有找到', 404);
                }

                //修改时检查是否重复
                $sql = "SELECT face_value_id FROM card_face_value WHERE face_value_id != ? AND face_value = ?";
                $item_temp_array = $G_SHOP->DBCA->stmt_query( $sql, 'is', array($face_value_id, $face_value) );

                if ($item_temp_array) {
                    throw new Exception('现金卡面值已存在', 404);
                }
            }

            //保存代金卡信息
            $card_master->card_info = $card_info;
            //保存代金卡信息
            $card_master->face_value = $face_value;

            $G_SHOP->DBCA->save( $card_master ) ;

            if ($face_value_id > 0) {
                return array( 'code' => 200, 'message' => '现金卡修改成功' );
            } else {
                return array( 'code' => 200, 'message' => '现金卡保存成功' );
            }

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }


    /**
     * 删除现金卡
     * @param array $pay_array 现金卡ID数组
     * @return array
     */
    public function deleteCashCard ( $pay_array ) {

        global $G_SHOP;

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $G_SHOP->DBCA->beginTransaction();

            foreach($pay_array as &$face_value_id) {



                //查询代金卡方式
                $pay_obj = new entity('card_face_value');
                $pay_obj->face_value_id = $face_value_id;
                if (!$G_SHOP->DBCA->fetch($pay_obj)) {
                    throw new Exception('现金卡信息没有找到', 404);
                }



                //删除现金卡信息
                $G_SHOP->DBCA->delete($pay_obj);

            }

            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message' => '删除现金卡成功' );

        } catch ( Exception $e ) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

}
